﻿Public Class CINNERLabelState2 : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERLabelState2.IsEnter")
        If CCommon.isInStr(task.ActionCd, "INNER_LABEL") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Debug.WriteLine("CINNERLabelState2.START")
        Dim nextState As CStateBase = Nothing
        Dim pdaTask As CPDATask2 = DirectCast(task, CPDATask2)
        Dim webMng As CWebManager = CWebManager.GetInstance
        Dim AppManager As CApplicationManager = CApplicationManager.GetInstance
        Dim checkShpData As CCheck = CCheck.GetIns

        'MessageBox.Show("INNER_STATE")
        Try

        
            Dim app As CApplication = CApplication.GetInstance()
            Dim sql As CSQLite = app.m_MasterDB
            Dim masterDao As New CMasterDAO
            Dim trx As CTransaction = sql.GetTransaction
            'Dim actionQty As Integer = 0

            'エラー  ->Insertするデータが無い
            If pdaTask.NowOrderDataList.Count = 0 Then
                'MessageBox.Show("NowOrderDataList.Count = 0")

            Else
                Dim setCd As String = pdaTask.ProcessID & "-" & System.Environment.TickCount.ToString()
                'INSERTする============================>
                For i As Integer = 0 To pdaTask.NowOrderDataList.Count - 1
                    '子ラベルデータ
                    Dim labelData As CPersonal.strctNowLabelState = pdaTask.NowOrderDataList(i)

                    '親出荷データをINSERTする============================START>
                    If i = 0 Then
                        Dim ParentLbl As CPersonal.OrderDataTbl = New CPersonal.OrderDataTbl
                        ParentLbl.ShiporderCd = pdaTask.ShiporderCd
                        ParentLbl.ActionEmpCd = pdaTask.EmpCd
                        ParentLbl.orderCd = pdaTask.OrderCd
                        ParentLbl.rowNum = pdaTask.RowNum
                        ParentLbl.reportCd = pdaTask.ReportCd
                        ParentLbl.orderQty = pdaTask.ShiporderQty
                        ParentLbl.itemCd = pdaTask.ItemCd
                        ParentLbl.sysitemCd = pdaTask.SysitemCd

                        ParentLbl.IsSend = "0" '0->未送信     1->送信済
                        ParentLbl.actionDateTaime = Now
                        ParentLbl.linkKey = "SH-" & setCd
                        ParentLbl.setCd = setCd

                        'INSERT SQL実行
                        'MessageBox.Show("InsertShpParentData--START")
                        masterDao.InsertShpParentData(trx, ParentLbl, pdaTask)
                        'MessageBox.Show("InsertShpParentData-END")
                        '親出荷データをINSERTする============================END>


                        'ラベル照合親テーブルにINSERTするために============================START>
                        Dim ParentLblData As CCheck.LabelData = New CCheck.LabelData
                        ParentLblData.setCd = setCd

                        ParentLblData.orderCd = pdaTask.OrderCd
                        ParentLblData.reserveCd = labelData.ReserveCd
                        ParentLblData.invoiceCd = labelData.InvoiceCd
                        ParentLblData.sysitemCd = pdaTask.SysitemCd
                        ParentLblData.grpPattern = labelData.GrpPattern
                        ParentLblData.isSend = "0"
                        ParentLblData.itemCd = pdaTask.ItemCd
                        ParentLblData.orderQty = pdaTask.ShiporderQty
                        ParentLblData.actionQty = labelData.ActionQty

                        ParentLblData.record1 = labelData.Record1
                        ParentLblData.record2 = labelData.Record2
                        ParentLblData.record3 = labelData.Record3
                        ParentLblData.subinvoiceCd1 = labelData.SubInvoiceCd1
                        ParentLblData.subinvoiceCd2 = labelData.SubInvoiceCd2
                        ParentLblData.subinvoiceCd3 = labelData.SubInvoiceCd3
                        ParentLblData.subinvoiceCd4 = labelData.SubInvoiceCd4
                        ParentLblData.isUsed = "1"
                        ParentLblData.lbCate = "0"      ''----->>ラベル照合のみは１　　出荷照合のみは０
                        ParentLblData.shCate = "1"      ''----->>出荷照合のみは１　　ない場いは０

                        ParentLblData.grpPattern = pdaTask.PatternList(0)
                        'MessageBox.Show("parentlbl")
                        Dim ret1 As Integer = masterDao.InsertLblData(trx, ParentLblData, pdaTask)
                        'MessageBox.Show("parentlbl_insert:" & ret1)

                        'ラベル照合親テーブルにINSERTするために============================END>

                    End If
                    '子ラベルデータ
                    'If actionQty = 0 Then
                    '    'actionQty = labelData.ActionQty
                    '    actionQty = CInt(pdaTask.LabelQty)
                    'End If
                    'MessageBox.Show("shiporderData:" & labelData.ShipOrderCd)
                    labelData.ShipOrderCd = pdaTask.ShiporderCd
                    labelData.OrderCd = pdaTask.OrderCd
                    labelData.RowNum = pdaTask.RowNum
                    labelData.ShiporderQty = pdaTask.ShiporderQty
                    labelData.SetCd = setCd
                    labelData.ProcId = pdaTask.ProcessID
                    labelData.ProcSeq = pdaTask.ProcSeq
                    labelData.GrpPattern = pdaTask.PatternList(0)
                    labelData.ShiporderQty = pdaTask.ShiporderQty
                    labelData.OrderQty = pdaTask.ShiporderQty

                    pdaTask.ProcSeq += 1

                    labelData.EmpCd = pdaTask.EmpCd
                    'labelData.UniqueCd = pdaTask.UniqueCd

                    'MessageBox.Show("InsertShpDetailData")
                    Dim ret As Integer = masterDao.InsertShpDetailData(trx, labelData, pdaTask)
                    'MessageBox.Show("InsertShpDetailData:" & ret)
                    '子ラベルデータをINSERTする============================END>


                    'ラベル照合子テーブルにINSERTする============================START>
                    Dim ChdLblData As CCheck.ChdLabelInfo = New CCheck.ChdLabelInfo

                    ChdLblData.procId = pdaTask.ProcessID
                    ChdLblData.procSeq = pdaTask.ProcSeq - 1  '-->上が既プラスなってて
                    ChdLblData.setCd = setCd
                    ChdLblData.readText = labelData.ReadText
                    ChdLblData.uniqueCd = labelData.UniqueCd
                    ChdLblData.labelNm = labelData.LabelNm
                    ChdLblData.isFraction = labelData.IsFraction        '０：普通のラベル　１：端数ラベル
                    ChdLblData.labelQty = labelData.LabelQty
                    ChdLblData.labelQty = labelData.ActionQty
                    ChdLblData.empCd = labelData.EmpCd
                    ChdLblData.itemCd = labelData.ItemCd
                    ChdLblData.chd_record1 = labelData.Record1
                    ChdLblData.chd_record2 = labelData.Record2
                    ChdLblData.chd_record3 = labelData.Record3
                    ChdLblData.actionDateTime = Now 'labelData.ReadDateTime
                    ChdLblData.chd_subinvoiceCd1 = labelData.SubInvoiceCd1
                    ChdLblData.chd_subinvoiceCd2 = labelData.SubInvoiceCd2
                    ChdLblData.chd_subinvoiceCd3 = labelData.SubInvoiceCd3
                    ChdLblData.chd_subinvoiceCd4 = labelData.SubInvoiceCd4
                    ChdLblData.actionInboxQty = labelData.ActionQty
                    ChdLblData.lbCate = "0"                     ''----->>０：ラベル照合なし   １：ラベル照合のみ
                    ChdLblData.shCate = "1"                     ''----->>０：出荷照合なし   １：出荷照合のみ
                    ChdLblData.linkKey = "SH-" & setCd

                    If masterDao.InsertChdLblData(trx, ChdLblData, pdaTask.PatternList(0), pdaTask) Then
                    End If
                    'ラベル照合子テーブルにINSERTする============================END>

                    If labelData.UniqueCd <> "" Then
                        'MessageBox.Show("uniqueInsert")
                        'ユニークコードINSERTする
                        Dim uniqueData As CCheck.UniqueLblData = New CCheck.UniqueLblData
                        uniqueData.actionDateTime = Now
                        uniqueData.empCd = pdaTask.EmpCd
                        uniqueData.uniqueCd = labelData.UniqueCd

                        'INSERT終わったら前の情報は消す
                        checkShpData.add_AllShUniData(labelData.UniqueCd)

                        '出荷で使ったラベル
                        masterDao.InsertShpUniqueData(trx, uniqueData, pdaTask)
                        'ラベル照合のユニークコード
                        masterDao.InsertUniqueData(trx, uniqueData, pdaTask)

                        'ADD
                        checkShpData.add_AllUniData(uniqueData.uniqueCd)
                        checkShpData.add_PartUniData(uniqueData.uniqueCd)
                    End If

                Next

                'ユニークコードADD

                '途中取消されたらINSERTしたものは全部消す
                pdaTask.PartSetCd.Add(setCd)
                checkShpData.add_AllShpData(setCd)

                '追加10.09.15--->>>START
                checkShpData.add_PartLblData(setCd)
                checkShpData.add_AllLblData(setCd)
                '追加10.09.15--->>>END

                'INSERT終わったら前の情報は消す
                'pdaTask.SetPattern = ""
                pdaTask.MsgText = ""
            End If


            '画面に表示する
            'MessageBox.Show("pdaTask.OrderCd:" & pdaTask.OrderCd)


            'Dim setData As COrderRecord = pdaTask.WindowManager.SetCurrentOrder(pdaTask.OrderCd)

            'MessageBox.Show("ActionQty:" & pdaTask.LabelQty)
            Debug.WriteLine("LABEL_ActionQty:" & pdaTask.LabelQty)

            'Debug.WriteLine("setData.ActionQty:" & setData.ActionQty)
            'setData.ActionQty += CInt(pdaTask.LabelQty)
            'setData.ActionBoxQty += 1
            'setData.ActionQty += pdaTask.ActionQty
            'setData.OrderCd = InvoceOrder.invoiceCd
            'pdaTask.WindowManager.RefreshPage()


            '自動的にページに移動する
            Dim pageLogic As Boolean = True
            For i As Integer = 0 To pdaTask.WindowManager.GetRecordCount - 1
                Dim recordBean As COrderRecord = pdaTask.WindowManager.GetRecord(i)
                If recordBean.OrderCd = pdaTask.OrderCd Then
                    '1226修正ーーーSTART
                    'recordBean.ActionQty = pdaTask.ActionQty
                    '1226修正------END
                    'MessageBox.Show("actQTY:" & masterDao.GetOrderData(trx, pdaTask.OrderCd))
                    'CFileLog.WriteLog("エラー落ちるかもー０７")
                    SyncLock pdaTask.SendLockObject
                        recordBean.ActionQty = masterDao.GetOrderData(trx, pdaTask.OrderCd)
                        'MessageBox.Show("実績数:" & recordBean.ActionQty)
                        pdaTask.WindowManager.SelectOrder(recordBean.OrderCd)
                    End SyncLock

                    pageLogic = False
                    Exit For
                End If
                pageLogic = True
            Next

            If pageLogic Then
                SyncLock pdaTask.SendLockObject
                    pdaTask.WindowManager.SetPage(pdaTask.OrderCd)
                End SyncLock

            End If

            'データ更新
            'CFileLog.WriteLog("エラー落ちるかもー０８")
            SyncLock pdaTask.SendLockObject
                Dim aaa As Integer = masterDao.UpdateShpOrderEND(trx, pdaTask.OrderCd, masterDao.GetOrderData(trx, pdaTask.OrderCd))
            End SyncLock


            'MessageBox.Show("aaa:" & aaa)

            'Dim pageId As Integer = StateDao.GetPageId(trx, shpOrderData.OrderCd)
            'MessageBox.Show("11")
            '絞るリストは削除
            pdaTask.WindowManager.ReleaseReport()         '->出荷情報絞ったリスト
            pdaTask.WindowManager.ReleaseLabelCheckList() '->ラベル絞ったリスト


            '一回の照合パタンが終わりのフラグ
            CCommon.FlagManage("INSERT_FLG", True)
            '画面用
            For j As Integer = 0 To pdaTask.WindowManager.GetRecordCount - 1
                Dim record As COrderRecord = pdaTask.WindowManager.GetRecord(j)
                'Debug.WriteLine("record.OrderCd:" & record.OrderCd)
                'Debug.WriteLine("pdaTask.OrderCd :" & pdaTask.OrderCd)
                If record.OrderCd = pdaTask.OrderCd And record.ActionQty = record.ShiporderQty Then
                    '↓これも設定から読み込みときに消えるか照合が終わったら消えるかの設定
                    ''----->>照合が終わったらリストから削除するロジック----->>START
                    If AppManager.m_AppSetting.ListDeleteChk Then
                        pdaTask.DelOrderData.Add(record)
                        'MessageBox.Show("まず、消えません")
                        Debug.WriteLine("まず、消えません")
                    Else
                        record.Visible = False
                        pdaTask.WindowManager.DelRecord(record, True)
                        'MessageBox.Show("すぐ、消えます。")
                        Debug.WriteLine("すぐ、消えます。")
                    End If
                    Exit For
                End If
            Next

            'S003 パタンは
            If Not pdaTask.PatternList(0) = "S003" Then
                '照合後全部消す
                CPersonal.initPdaTaskSetting(pdaTask)

                pdaTask.PatternList = New ArrayList
                pdaTask.LabelCount = 1
                pdaTask.ConfirmOrderCd = New ArrayList

            End If

            '分納で終わりもの
            pdaTask.ActionQty = 0
            pdaTask.UniqueCd = ""
            pdaTask.NowOrderDataList = New ArrayList

            pdaTask.LabelQty = 0
            pdaTask.PartLabelQty = 0
            pdaTask.ChdLabelQty = 0

        Catch ex As Exception
            CFileLog.WriteLog("INNER_CHECK-エラー-：" & ex.Message)
        End Try

        task.ActionCd = "LABEL"
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CLabelCheckState.DoExit")
        Return True
    End Function


End Class
